package solution.design;

import java.util.HashMap;
import java.util.Map;

/**
 * @author zhangmin
 * @create 2021-10-29 11:08
 * 设计问题：170 两数之和3
 */
public class TwoSum {

    Map<Integer,Integer> freq=new HashMap<>();
    // 向数据结构中添加一个数 number
    public void add(int number){
        freq.put(number,freq.getOrDefault(number,0)+1);
    }
    // 寻找当前数据结构中是否存在两个数的和为 value
    public boolean find(int value){
        for (int key:freq.keySet()) {
            int other=value-key;
            if (other==key&&freq.get(key)>1)
                return true;
            if (other!=key&&freq.containsKey(other))
                return true;
        }
        return false;
    }
}
